<html lang="en">
	<head>
		<title>Yuka | Steering Behaviors | Wander</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			"Wander" produces a steering force that will give the impression of a random walk through the agent’s environment.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';

		let renderer, scene, camera;

		let entityManager, time;

		init();
		animate();

		function init() {

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
			camera.position.set( 0, 20, 0 );
			camera.lookAt( scene.position );

			// game setup

			entityManager = new YUKA.EntityManager();
			time = new YUKA.Time();

			//

			const vehicleGeometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
			vehicleGeometry.rotateX( Math.PI * 0.5 );
			const vehicleMaterial = new THREE.MeshNormalMaterial();

			for ( let i = 0; i < 50; i ++ ) {

				const vehicleMesh = new THREE.Mesh( vehicleGeometry, vehicleMaterial );
				vehicleMesh.matrixAutoUpdate = false;
				scene.add( vehicleMesh );

				const vehicle = new YUKA.Vehicle();
				vehicle.rotation.fromEuler( 0, 2 * Math.PI * Math.random(), 0 );
				vehicle.position.x = 2.5 - Math.random() * 5;
				vehicle.position.z = 2.5 - Math.random() * 5;
				vehicle.setRenderComponent( vehicleMesh, sync );

				const wanderBehavior = new YUKA.WanderBehavior();
				vehicle.steering.add( wanderBehavior );

				entityManager.add( vehicle );

			}

			const grid = new THREE.GridHelper( 20, 50 );
			scene.add( grid );

			//

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			window.addEventListener( 'resize', onWindowResize, false );

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}

		function animate() {

			requestAnimationFrame( animate );

			const delta = time.update().getDelta();

			entityManager.update( delta );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}

	</script>

</body>
</html>
